<?
	include("include/common.php");

	if(!$loggedin){
		ob_start();
		header("Location: login.php");
	}
	include("include/header.php");
	include("include/accmenu.php");

	if($submit){
		if($_POST['delimg']){
			mysql_query("UPDATE portfolio SET imagefile='' WHERE id='$oid'");
			if(file_exists("./uploads/".$_POST['delimg']))@unlink("./uploads/".$_POST['delimg']);
		}
		if($_POST['delimg2']){
			mysql_query("UPDATE portfolio SET imagefile2='' WHERE id='$oid'");
			if(file_exists("./uploads/".$_POST['delimg2']))@unlink("./uploads/".$_POST['delimg2']);
		}
		if($_POST['delimg3']){
			mysql_query("UPDATE portfolio SET imagefile3='' WHERE id='$oid'");
			if(file_exists("./uploads/".$_POST['delimg3']))@unlink("./uploads/".$_POST['delimg3']);
		}
		if($_POST['delimg4']){
			mysql_query("UPDATE portfolio SET imagefile4='' WHERE id='$oid'");
			if(file_exists("./uploads/".$_POST['delimg4']))@unlink("./uploads/".$_POST['delimg4']);
		}
		if($oid){
			$sql = "UPDATE portfolio SET title='$ctitle',body='$cbody'";
			if( $_FILES['image']['name'] ){
				$sql .= ",imagefile='".$_FILES['image']['name']."'";
			}
			if( $_FILES['image2']['name'] ){
				$sql .= ",imagefile2='".$_FILES['image2']['name']."'";
			}
			if( $_FILES['image3']['name'] ){
				$sql .= ",imagefile3='".$_FILES['image3']['name']."'";
			}
			if( $_FILES['image3']['name'] ){
				$sql .= ",imagefile4='".$_FILES['image4']['name']."'";
			}
			$sql .= " WHERE id='$oid'";
		}else{
			$sql = "INSERT INTO portfolio SET title='$ctitle',body='$cbody',user='$myuid'";
			if( $_FILES['image']['name'] ){
				$sql .= ",imagefile='".$_FILES['image']['name']."'";
			}
			if( $_FILES['image2']['name'] ){
				$sql .= ",imagefile2='".$_FILES['image2']['name']."'";
			}
			if( $_FILES['image3']['name'] ){
				$sql .= ",imagefile3='".$_FILES['image3']['name']."'";
			}
			if( $_FILES['image3']['name'] ){
				$sql .= ",imagefile4='".$_FILES['image4']['name']."'";
			}
		}
//		echo $sql."<br>";
		mysql_query($sql) or die( mysql_error()."<br>$sql<bR>" );
		if(!$oid){
			$oid = mysql_insert_id();
		}
		if( $_FILES['image']['name'] ){
			if($_POST['oldimg'])@unlink("./uploads/".$_POST['oldimg']);
			$match=0;
			$sent=move_uploaded_file($_FILES['image']['tmp_name'], "./uploads/".$_FILES['image']['name']);
			if(!$sent){
				echo ("<script>window.alert('File upload has failed.');</script>");
			}else{
				echo ("<script>window.alert('File upload of file type $type was sucessfull');</script>");
			}
		}
		if( $_FILES['image2']['name'] ){
			if($_POST['oldimg2'])@unlink("./uploads/".$_POST['oldimg2']);
			$sent=move_uploaded_file($_FILES['image2']['tmp_name'], "./uploads/".$_FILES['image2']['name']);
			if(!$sent){
				echo ("<script>window.alert('File upload has failed.');</script>");
			}else{
				echo ("<script>window.alert('File upload of file type $type was sucessfull');</script>");
			}
		}
		if( $_FILES['image3']['name'] ){
			if($_POST['oldimg3'])@unlink("./uploads/".$_POST['oldimg3']);
			$sent=move_uploaded_file($_FILES['image3']['tmp_name'], "./uploads/".$_FILES['image3']['name']);
			if(!$sent){
				echo ("<script>window.alert('File upload has failed.');</script>");
			}else{
				echo ("<script>window.alert('File upload of file type $type was sucessfull');</script>");
			}
		}
		if( $_FILES['image4']['name'] ){
			if($_POST['oldimg4'])@unlink("./uploads/".$_POST['oldimg4']);
			$sent=move_uploaded_file($_FILES['image4']['tmp_name'], "./uploads/".$_FILES['image4']['name']);
			if(!$sent){
				echo ("<script>window.alert('File upload has failed.');</script>");
			}else{
				echo ("<script>window.alert('File upload of file type $type was sucessfull');</script>");
			}
		}		

?>
		<h3>Edit Portfolio Item</h3>
		<?=$table2?>
		<tr align=center>
			<td colspan=2>Portfolio Item has been saved.</td>
		</tr>
<?
		echo "</table>";
	}else{
		if($oid){
			$this->c=mysql_query("select * from portfolio where id='$oid'");
			$this->d=mysql_fetch_object($this->c);
			if(is_object($this->d)) {
				$cid = $this->d->id;
				$ctitle = $this->d->title;
				$cbody = $this->d->body;
				$imagefile = $this->d->imagefile;
				$imagefile2 = $this->d->imagefile2;
				$imagefile3 = $this->d->imagefile3;
				$imagefile4 = $this->d->imagefile4;
			}
		}
?>
		<h3>Edit Portfolio Item</h3>
		<form method=post ENCTYPE="multipart/form-data">
<?		if($oid)echo "<input type='hidden' name='oid' value='$oid'";	?>
		<?=$table2?>
		<tr>
			<td colspan=2>Edit your portfolio item and then click the submit button below.<br><br></td>
		</tr>
		<tr>
			<td>Title:</td>
			<td><input type="text" name="ctitle" value="<?=$ctitle?>">
		<tr>
			<td>About This Item:</td>
			<td>
				<textarea name=cbody cols=50 rows=10><?=$cbody?></textarea>
			</td>
		</tr>
		<tr>
			<td>Image 1:</td>
			<td>
				<INPUT NAME="image" TYPE="file">
<?
	if($oid && $imagefile){
		echo "&nbsp; 
			<input type=hidden name=oldimg value='$imagefile'> 
			<input type=checkbox class=checkbox name=delimg value='$imagefile'> 
			Delete image (<a href='$siteurl/uploads/$imagefile","' target=_blank>See current</a>)";
	}
?>
			</td>
		</tr>
		<tr>
			<td>Image 2:</td>
			<td>
				<INPUT NAME="image2" TYPE="file">
<?
	if($oid && $imagefile2){
		echo "&nbsp; 
			<input type=hidden name=oldimg2 value='$imagefile2'> 
			<input type=checkbox class=checkbox name=delimg2 value='$imagefile2'> 
			Delete image (<a href='$siteurl/uploads/$imagefile2","' target=_blank>See current</a>)";
	}
?>
			</td>
		</tr>
		<tr>
			<td>Image 3:</td>
			<td>
				<INPUT NAME="image3" TYPE="file">
<?
	if($oid && $imagefile3){
		echo "&nbsp; 
			<input type=hidden name=oldimg3 value='$imagefile3'> 
			<input type=checkbox class=checkbox name=delimg3 value='$imagefile3'> 
			Delete image (<a href='$siteurl/uploads/$imagefile3","' target=_blank>See current</a>)";
	}
?>
			</td>
		</tr>
		<tr>
			<td>Image 4:</td>
			<td>
				<INPUT NAME="image4" TYPE="file">
<?
	if($oid && $imagefile4){
		echo "&nbsp; 
			<input type=hidden name=oldimg4 value='$imagefile4'> 
			<input type=checkbox class=checkbox name=delimg4 value='$imagefile4'> 
			Delete image (<a href='$siteurl/uploads/$imagefile4","' target=_blank>See current</a>)";
	}
?>
			</td>
		</tr>

		<tr>
			<td> </td><td><input type=submit name=submit value='Submit Changes'></td></tr>
		</table>
		</form>
<?
	}
	include("include/footer.php");
?>